package com.fortune.controller.app;

import com.faneui.common.bean.BaseResp;
import com.faneui.common.exception.BusinessException;
import com.faneui.common.utils.ListUtils;
import com.fortune.dal.dao.CustDAO;
import com.fortune.dal.model.Cust;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;
import java.util.List;

@RestController
@RequestMapping("securitycenter")
public class SecurityCenterController {

    @Autowired
    private HttpSession httpSession;

    @Autowired
    private CustDAO custDAO;

    @RequestMapping(value = "", method = RequestMethod.GET)
    public ModelAndView indexOfAll() throws Exception {
        ModelAndView mv = new ModelAndView("securitycenter");
        mv.addObject("menuFlag", "securitycenter");
        return mv;
    }

    @RequestMapping(value = "/changelogpasswordfir", method = RequestMethod.GET)
    public ModelAndView indexOfAllOne() throws Exception {
        ModelAndView mv = new ModelAndView("changelogpasswordfir");
        mv.addObject("menuFlag", "changelogpasswordfir");
        return mv;
    }

    @RequestMapping(value = "/changelogpasswordsec", method = RequestMethod.GET)
    public ModelAndView indexOfAllTwo() throws Exception {
        ModelAndView mv = new ModelAndView("changelogpasswordsec");
        mv.addObject("menuFlag", "changelogpasswordsec");
        return mv;
    }


    @RequestMapping(value = "/changepassword", method = RequestMethod.POST)
    @ResponseBody
    public BaseResp changePassword(@RequestBody Cust cust) throws BusinessException {
        BaseResp resp = new BaseResp();
        try {
            if(StringUtils.isBlank(cust.getPassword())){
                throw new BusinessException("密码不能为空");
            }
            if(StringUtils.isBlank(cust.getPhone())){
                throw new BusinessException("用户未登录");
            }
            // 先检查下 username 是否存在
            Cust param = new Cust();
            param.setPhone(cust.getPhone());
            List<Cust> custs = custDAO.queryByModel(param) ;
            if(ListUtils.isEmpty(custs)) {
                throw new BusinessException("用户未登录") ;
            }
            // 修改密码
            Cust c = custs.get(0) ;
            c.setPassword(cust.getPassword());
            if(c.getId() == null) {
                throw new BusinessException("修改密码失败");
            }
            custDAO.updateById(c);
        } catch (Exception ex) {
            resp.setSuccess(false);
            resp.setMsg(ex.getMessage());
        }
        return resp;
    }

    @RequestMapping(value = "/changelogpasswordsuccess", method = RequestMethod.GET)
    public ModelAndView indexOfAllThree() throws Exception {
        ModelAndView mv = new ModelAndView("changelogpasswordsuccess");
        mv.addObject("menuFlag", "changelogpasswordsuccess");
        return mv;
    }


    @RequestMapping(value = "/changetradepassfir", method = RequestMethod.GET)
    public ModelAndView changeTradePassFir() throws Exception {
        ModelAndView mv = new ModelAndView("changetradepassfir");
        mv.addObject("menuFlag", "changetradepassfir");
        return mv;
    }

    //验证输入的原交易密码是否正确
    @RequestMapping(value = "/checktradepassword", method = RequestMethod.POST)
    public BaseResp checkTradePassword(@RequestBody Cust cust) throws Exception {
        BaseResp resp = new BaseResp();
        try{
            if(cust.getTradePassword() == ""){
                throw new BusinessException("未输入密码");
            }

           Cust cust1 = (Cust)httpSession.getAttribute("user");
           if(!(cust.getTradePassword().equals(cust1.getTradePassword()))){
               throw new BusinessException("输入密码不正确");
           }

        }catch(Exception ex){
            resp.setSuccess(false);
            resp.setMsg(ex.getMessage());
        }
        return resp;
    }


    @RequestMapping(value = "/changetradepasssec", method = RequestMethod.GET)
    public ModelAndView changeTradePassSec() throws Exception {
        ModelAndView mv = new ModelAndView("changetradepasssec");
        mv.addObject("menuFlag", "changetradepasssec");
        return mv;
    }

    //修改交易密码
    @RequestMapping(value = "/updatetradepassword", method = RequestMethod.POST)
    public BaseResp updateTradePassword(@RequestBody Cust cust) throws Exception {
        BaseResp resp = new BaseResp();
        try{
            if(cust.getTradePassword() == ""){
                throw new BusinessException("新的交易密码为空");
            }
            Cust cust1 = (Cust)httpSession.getAttribute("user");
            cust1.setTradePassword(cust.getTradePassword());
            custDAO.updateById(cust1);

        }catch(Exception ex){
            resp.setSuccess(false);
            resp.setMsg(ex.getMessage());
        }

        return resp;
    }

    //修改交易密码成功
    @RequestMapping(value = "/changetradepass", method = RequestMethod.GET)
    public ModelAndView changeTradePassSuccess() throws Exception {
        ModelAndView mv = new ModelAndView("changetradepasssuccess");
        mv.addObject("menuFlag", "changetradepass");
        return mv;
    }

}
